HAL PWR typical use cases for migration

HAL PWR Enter Stop Mode with RTC Wakeup

@startuml

title Enter Stop Mode with RTC Wakeup


participant "Application Code" as App
participant "HAL Library" as HAL
participant "PWR Peripheral" as PWR
participant "RTC Peripheral" as RTC


App -> HAL: HAL_Init()
App -> HAL: SystemClock_Config()
App -> HAL: __HAL_RCC_PWR_CLK_ENABLE()

App -> HAL: HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1)
HAL -> PWR: Configure Voltage Scaling

App -> HAL: HAL_RTC_Init(&hrtc)
HAL -> RTC: Initialize RTC

App -> HAL: HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, timeout, clock)
HAL -> RTC: Configure Wakeup Timer

alt Enter stop0 mode
    App -> HAL: HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI)
else Enter stop2 mode
    App -> HAL:HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI)
end

HAL -> PWR: Enter Stop Mode
note right of PWR
CPU and peripherals stopped.
RAM and registers retained.
end note

PWR -> HAL: Wait for Wakeup Event
RTC -> HAL: Wakeup Event Triggered

HAL -> PWR: Exit Stop Mode
note right of PWR
Resume user code execution.
end note
App -> HAL: SystemClock_Config()
HAL -> PWR: Reconfigure System Clock



@enduml

HAL1

HAL2

Usage

// RTC handle
RTC_HandleTypeDef hrtc;

void SystemClock_Config(void)
{
    // Reinitialize the system clock after waking up
    // (Implementation depends on your specific clock configuration)
    // Example: Re-enable HSE or HSI and configure PLL
}

int main(void)
{
    // HAL initialization
    HAL_Init();

    // Configure the system clock
    SystemClock_Config();

    // Initialize RTC
    hrtc.Instance = RTC;
    HAL_RTC_Init(&hrtc);

    // Enable PWR clock
    __HAL_RCC_PWR_CLK_ENABLE();

    // Configure Voltage Scaling (Scale 1 for low-power modes)
    HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

    // Configure the RTC as a wakeup source
    HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0xFFFF, RTC_WAKEUPCLOCK_RTCCLK_DIV16 , 0);

    // Enter Stop Mode (Stop 0 with low-power regulator)
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

    // System resumes here after wakeup
    SystemClock_Config(); // Reconfigure the system clock

    while (1)
    {
        // Main loop
    }
}
hal_rtc_wakeup_config_t config_wakeup;
hal_rtc_time_t period_reload;
hal_rtc_time_t period_autoclear;

void SystemClock_Config(void)
{
    // Reinitialize the system clock after waking up
    // (Implementation depends on your specific clock configuration)
    // Example: Re-enable HSE or HSI and configure PLL
}

int main(void)
{
    // HAL initialization
    HAL_Init();

    // Configure the system clock
    SystemClock_Config();

    // Initialize RTC
    HAL_RTC_DisableWriteProtection()
    HAL_RTC_EnterInitMode()

    // Enable PWR clock
    HAL_RCC_PWR_EnableClock();

    // Configure Voltage Scaling (Scale 1 for low-power modes)
    HAL_PWR_SetMainRegulatorVoltageScalingRange(HAL_PWR_VOLT_SCALE_1);

    // Configure the RTC as a wakeup source
    config_wakeup.clock = HAL_RTC_WAKEUP_TIMER_CLOCK_RTCCLK_DIV16;
    period_reload.sec = 1U;

    HAL_RTC_WAKEUP_SetConfig(&config_wakeup);
    HAL_RTC_WAKEUP_SetPeriod(&period_reload, &period_autoclear);
    HAL_RTC_WAKEUP_Start(HAL_RTC_WAKEUP_IT_ENABLE);

    // Enter Stop Mode (Stop 0 with low-power regulator)
    HAL_PWR_EnterSTOPMode(HAL_PWR_LOW_PWR_MODE_WFI, HAL_PWR_STOP0_MODE);

    // System resumes here after wakeup
    SystemClock_Config(); // Reconfigure the system clock

    while (1)
    {
        // Main loop
    }
}

HAL PWR Enter Stop Mode with External Wakeup

@startuml
title Enter Stop Mode with External Wakeup


participant "Application Code" as App
participant "HAL Library" as HAL
participant "PWR Peripheral" as PWR
participant "GPIO Peripheral" as GPIO
participant "EXTI (Interrupt)" as EXTI

User -> App: Start Application
App -> HAL: HAL_Init()
App -> HAL: SystemClock_Config()
App -> HAL: __HAL_RCC_PWR_CLK_ENABLE()

App -> HAL: HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1)
HAL -> PWR: Configure Voltage Scaling

App -> HAL: __HAL_RCC_GPIOA_CLK_ENABLE()
App -> HAL: HAL_GPIO_Init(WAKEUP_PORT, &GPIO_InitStruct);
HAL -> GPIO: Configure GPIO Pin (e.g., PA0)
HAL -> EXTI: Enable EXTI Line for GPIO Pin

App -> HAL: HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI)
HAL -> PWR: Enter Stop Mode
note right of PWR
CPU and peripherals stopped.
RAM and registers retained.
end note

PWR -> HAL: Wait for External Interrupt
User -> GPIO: Trigger External Interrupt (e.g., Button Press)
GPIO -> EXTI: Generate Interrupt Signal
EXTI -> HAL: EXTI Interrupt Handler (EXTI0_IRQHandler)

HAL -> App: HAL_GPIO_EXTI_Callback()
App -> HAL: Handle Wakeup Event

HAL -> PWR: Exit Stop Mode
App -> HAL: SystemClock_Config()
HAL -> PWR: Reconfigure System Clock

App -> User: Resume Normal Operation

@enduml

HAL1

HAL2

Usage

// Define the GPIO pin for external wakeup (e.g., PA0)
#define WAKEUP_PIN GPIO_PIN_0
#define WAKEUP_PORT GPIOA

void SystemClock_Config(void);


int main(void)
{
    // Initialize the HAL Library
    HAL_Init();

    // Configure the system clock
    SystemClock_Config();

    // Enable the PWR clock
    __HAL_RCC_PWR_CLK_ENABLE();

    // Configure Voltage Scaling (Scale 1 for low-power modes)
    HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

    // Initialize the GPIO pin for external wakeup
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // Enable the GPIO clock for the wakeup pin
    __HAL_RCC_GPIOA_CLK_ENABLE();

    // Configure the GPIO pin as an external interrupt
    GPIO_InitStruct.Pin = WAKEUP_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; // Trigger on falling edge
    GPIO_InitStruct.Pull = GPIO_NOPULL;          // No pull-up or pull-down
    HAL_GPIO_Init(WAKEUP_PORT, &GPIO_InitStruct);

    // Enable and set the EXTI interrupt priority
    HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(EXTI0_IRQn);

    // Enter Stop Mode
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

    // System resumes here after wakeup
    SystemClock_Config(); // Reconfigure the system clock

    while (1)
    {
        // Main loop
    }
}

// EXTI interrupt handler for the wakeup pin
void EXTI0_IRQHandler(void)
{
    // Handle the interrupt
    HAL_GPIO_EXTI_IRQHandler(WAKEUP_PIN);
}

// Callback function for EXTI interrupt
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
      // Wakeup event occurred
      // Add any additional logic here if needed
}

void SystemClock_Config(void)
{
    // Reinitialize the system clock after waking up
    // (Implementation depends on your specific clock configuration)
    // Example: Re-enable HSE or HSI and configure PLL
}
// Define the GPIO pin for external wakeup (e.g., PA0)
#define WAKEUP_PIN HAL_GPIO_PIN_0
#define WAKEUP_PORT GPIOA

void SystemClock_Config(void);


int main(void)
{
    // Initialize the HAL Library
    HAL_Init();

    // Configure the system clock
    SystemClock_Config();

    // Enable the PWR clock
    HAL_RCC_PWR_EnableClock();

    // Configure Voltage Scaling (Scale 1 for low-power modes)
    HAL_PWR_SetMainRegulatorVoltageScalingRange(HAL_PWR_VOLT_SCALE_1);

    // Initialize the GPIO pin for external wakeup
    hal_gpio_config_t GPIO_ConfigStructure;
    hal_exti_handle_t hexti;
    hal_exti_config_t EXTI_ConfigStructure;

    GPIO_ConfigStructure.mode = HAL_GPIO_MODE_INPUT;
    GPIO_ConfigStructure.pull = HAL_GPIO_PULL_NO;


    HAL_GPIO_Init(WAKEUP_PORT, WAKEUP_PIN,
                             &GPIO_ConfigStructure);

    HAL_EXTI_Init(&hexti, HAL_EXTI_LINE_0);

    EXTI_ConfigStructure.trigger = HAL_EXTI_TRIGGER_FALLING;
    EXTI_ConfigStructure.gpio_port = HAL_EXTI_GPIOA;
    HAL_EXTI_SetConfig(&hexti, &EXTI_ConfigStructure);
    HAL_EXTI_Enable(&hexti, HAL_EXTI_MODE_INTERRUPT)


    HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(EXTI0_IRQn);

    // Enter Stop Mode
    HAL_PWR_EnterSTOPMode(HAL_PWR_LOW_PWR_MODE_WFI, HAL_PWR_STOP0_MODE);

    // System resumes here after wakeup
    SystemClock_Config(); // Reconfigure the system clock

    while (1)
    {
        // Main loop
    }
}

// EXTI interrupt handler for the wakeup pin
void EXTI0_IRQHandler(void)
{
    // Handle the interrupt
    HAL_EXTI_IRQHandler(&hexti);
}

// Callback function for EXTI interrupt
void HAL_EXTI_TriggerCallback(uint16_t GPIO_Pin)
{
      // Wakeup event occurred
      // Add any additional logic here if needed
}

void SystemClock_Config(void)
{
    // Reinitialize the system clock after waking up
    // (Implementation depends on your specific clock configuration)
    // Example: Re-enable HSE or HSI and configure PLL
}

Power Voltage Detector (PVD) with EXTI wakeup

@startuml

title PVD with EXTI Wakeup


participant "Main Application" as Main
participant "HAL Library" as HAL
participant "PVD Peripheral" as PVD
participant "EXTI Line" as EXTI
participant "NVIC" as NVIC

== Initialization ==

Main -> HAL: HAL_Init()
Main -> HAL: SystemClock_Config()
Main -> HAL: __HAL_RCC_PWR_CLK_ENABLE();
Main -> HAL: HAL_PWR_ConfigPVD(&sConfigPVD);
Main -> HAL: HAL_PWR_EnablePVD();


note right of HAL
- Configure PVD threshold
- Enable PVD interrupt
- Route PVD to EXTI
end note

HAL -> PVD: Configure PVD Level (e.g., 2.5V)
HAL -> NVIC: Enable PVD Interrupt

== Enter Sleep Mode ==
Main -> HAL: HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
HAL -> Main: Enter WFI (Wait for Interrupt)

== Voltage Threshold Crossed ==
Main -> PVD: Voltage crosses threshold
PVD -> EXTI: Trigger EXTI Line
EXTI -> NVIC: Generate Interrupt
NVIC -> HAL: Call PVD_AVD_IRQHandler()

== Interrupt Handling ==
HAL -> HAL: HAL_PWR_PVD_IRQHandler()
HAL -> Main: HAL_PWR_PVDCallback()

note right of Main
- Check PVD flag (PWR_FLAG_PVDO)
- Take action (e.g., toggle LED)
end note

Main -> HAL: Handle Voltage Event

== System Resumes ==
Main -> HAL: Resume normal operation

@enduml

HAL1

HAL2

Usage

#include "stm32u5xx_hal.h"

// Function Prototypes
void SystemClock_Config(void);

// Main Function
int main(void)
{
    // Initialize the HAL Library
    HAL_Init();

    // Configure the System Clock
    SystemClock_Config();

    // Configure the Power Voltage Detector (PVD)
    PWR_PVDTypeDef sConfigPVD;

    // Enable the PWR Clock
    HAL_RCC_PWR_EnableClock();

    // Configure the PVD Level and Mode
    sConfigPVD.PVDLevel = PWR_PVDLEVEL_3; // Set threshold (e.g., 2.5V)
    sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING_FALLING; // Interrupt on both rising and falling edges

    // Initialize the PVD
    HAL_PWR_ConfigPVD(&sConfigPVD);

    // Enable the PVD Interrupt
    HAL_PWR_EnablePVD();

    // Configure the EXTI Line for PVD
    HAL_NVIC_SetPriority(PVD_AVD_IRQn, 0, 0); // Set priority
    HAL_NVIC_EnableIRQ(PVD_AVD_IRQn);         // Enable the interrupt

    // Enter Sleep Mode and wait for PVD interrupt
    HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

    // Main loop (should not reach here unless woken up)
    while (1)
    {
        // Add your application code here
    }
}

// PVD Interrupt Handler
void HAL_PWR_PVDCallback(void)
{
    // This callback is triggered when the PVD detects a voltage threshold crossing
    if (__HAL_PWR_GET_FLAG(PWR_FLAG_PVDO))
    {
        // Voltage is below the threshold
    }
    else
    {
        // Voltage is above the threshold
    }
}

// System Clock Configuration (Example for STM32U5)
void SystemClock_Config(void)
{

}

// PVD IRQ Handler
void PVD_AVD_IRQHandler(void)
{
    HAL_PWR_PVD_IRQHandler();
}
#include "stm32u5xx_hal.h"

// Function Prototypes
void SystemClock_Config(void);

// Main Function
int main(void)
{
    // Initialize the HAL Library
    HAL_Init();

    // Configure the System Clock
    SystemClock_Config();

    // Enable the PWR Clock
    HAL_RCC_PWR_EnableClock();

    // Configure the PVD Level and Mode
    HAL_PWR_SetProgrammableVoltageDetectorLevel(HAL_PWR_PVD_LEVEL_3);

    // Initialize the PVD
    EXTI_ConfigStructure.trigger = HAL_EXTI_TRIGGER_RISING_FALLING;
    EXTI_ConfigStructure.gpio_port = HAL_EXTI_GPIOA;
    HAL_EXTI_SetConfig(&hexti, &EXTI_ConfigStructure);
    HAL_EXTI_Enable(&hexti, HAL_EXTI_MODE_INTERRUPT);

    // Enable the PVD Interrupt
    HAL_PWR_EnableProgrammableVoltageDetector();

    // Configure the EXTI Line for PVD
    HAL_NVIC_SetPriority(PVD_AVD_IRQn, 0, 0); // Set priority
    HAL_NVIC_EnableIRQ(PVD_AVD_IRQn);         // Enable the interrupt

    // Enter Sleep Mode and wait for PVD interrupt
    HAL_PWR_EnterSLEEPMode(HAL_PWR_LOW_PWR_MODE_WFI);

    // Main loop (should not reach here unless woken up)
    while (1)
    {
        // Add your application code here
    }
}

// PVD Interrupt Handler
void HAL_EXTI_TriggerCallback(void)
{
    // This callback is triggered when the PVD detects a voltage threshold crossing
    if (LL_PWR_IsActiveFlag_PVDO(LL_PWR_FLAG_PVDO))
    {
        // Voltage is below the threshold
    }
    else
    {
        // Voltage is above the threshold
    }
}

// System Clock Configuration (Example for STM32U5)
void SystemClock_Config(void)
{

}

void EXTI0_IRQHandler(void)
{
    HAL_EXTI_IRQHandler(&hexti);
}

HAL PWR Enter Standby Mode with External Wakeup

@startuml

title GPIO EXTI Wakeup with Standby Mode

participant "Main Application" as Main
participant "HAL Library" as HAL
participant "GPIO Peripheral" as GPIO
participant "EXTI Line" as EXTI
participant "NVIC" as NVIC
participant "PWR Peripheral" as PWR

== Initialization ==

Main -> HAL: HAL_Init()
Main -> HAL: SystemClock_Config()
Main -> GPIO: __HAL_RCC_GPIOA_CLK_ENABLE()
Main -> GPIO: HAL_GPIO_Init(GPIOA, &GPIO_InitStruct)

note right of GPIO
- Configure PA0 as input
- Set EXTI interrupt on rising edge
- No pull-up or pull-down
end note

Main -> NVIC: HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0)
Main -> NVIC: HAL_NVIC_EnableIRQ(EXTI0_IRQn)

Main -> PWR: __HAL_RCC_PWR_CLK_ENABLE()
Main -> PWR: __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU)
Main -> PWR: HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1_HIGH)

== Enter Standby Mode ==
Main -> PWR: HAL_PWR_EnterSTANDBYMode()
PWR -> Main: System enters Standby Mode

note right of Main
- Perform wakeup logic (e.g., toggle LED)
- Handle GPIO interrupt
end note

== System Reset ==
Main -> HAL: Resume from reset after waking up from standby mode

note right of Main
- Reinitialize system clock
- Reconfigure GPIOs
- Reinitialize peripherals (e.g., UART, I2C, etc.)
- Restore application state
- Resume tasks or processes
end note

@enduml

HAL1

HAL2

Usage

// Function prototypes
void SystemClock_Config(void);

int main(void)
{
    // HAL initialization
    HAL_Init();
    SystemClock_Config();

    // Initialize GPIO for EXTI
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // Enable GPIOA clock
    __HAL_RCC_GPIOA_CLK_ENABLE();

    // Configure PA0 as input with EXTI interrupt
    GPIO_InitStruct.Pin = GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; // Trigger on rising edge
    GPIO_InitStruct.Pull = GPIO_NOPULL;        // No pull-up or pull-down
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // Enable and set EXTI line 0 interrupt priority
    HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(EXTI0_IRQn);

    // Enter standby mode
    // Enable Power Control clock
    __HAL_RCC_PWR_CLK_ENABLE();

    // Clear the Wakeup flag
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);

    // Enable EXTI wakeup source
    HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1_HIGH);

    // Enter Standby mode
    HAL_PWR_EnterSTANDBYMode();

    // Code should never reach here after entering standby mode
    while (1)
    {
    }
}

// System Clock Configuration (example for STM32U5)
void SystemClock_Config(void)
{
    // Reinitialize the system clock after waking up
    // (Implementation depends on your specific clock configuration)
    // Example: Re-enable HSE or HSI and configure PLL
}
// Function prototypes
void SystemClock_Config(void);

int main(void)
{
    // HAL initialization
    HAL_Init();
    SystemClock_Config();


    // Enable GPIOA clock
    HAL_RCC_GPIOA_CLK_ENABLE();

    EXTI_ConfigStructure.trigger = HAL_EXTI_TRIGGER_RISING_FALLING;
    EXTI_ConfigStructure.gpio_port = HAL_EXTI_GPIOA;
    HAL_EXTI_SetConfig(&hexti, &EXTI_ConfigStructure);
    HAL_EXTI_Enable(&hexti, HAL_EXTI_MODE_INTERRUPT);

    // Enable and set EXTI line 0 interrupt priority
    HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(EXTI0_IRQn);

    // Enter standby mode
    // Enable Power Control clock
    HAL_RCC_PWR_EnableClock();

    // Clear the Wakeup flag
    LL_PWR_ClearFlag_WU1();

    // Enable EXTI wakeup source
    HAL_PWR_LP_SetConfigWakeupPin(HAL_PWR_WAKEUP_PIN_1, HAL_PWR_WAKEUP_PIN_POLARITY_HIGH)

    // Enter Standby mode
    HAL_PWR_EnterSTANDBYMode();

    // Code should never reach here after entering standby mode
    while (1)
    {
    }
}

// System Clock Configuration (example for STM32U5)
void SystemClock_Config(void)
{
    // Reinitialize the system clock after waking up
    // (Implementation depends on your specific clock configuration)
    // Example: Re-enable HSE or HSI and configure PLL
}